<?php
class Mailer
{
	public $toemail = 'web@isivy.com';
	public $subject = '发送测试';
	public $body = '收到这封邮件说明设置正确。';
	public $SetFromname = '义乌市常青藤软件';
	public $toname;
	public $mail_conf;
	public $taskname;

	private $email_users_qq = array(
			array('host'=>"smtp.qq.com",'setfrom'=>'web@isivy.com','username'=>'web@isivy.com','password'=>'changchangabc','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'qian@isivy.com','username'=>'qian@isivy.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s1@isivy.com','username'=>'xiaoqianjie_s1@isivy.com','password'=>'changchangabc','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s2@isivy.com','username'=>'xiaoqianjie_s2@isivy.com','password'=>'changchangabc','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s3@isivy.com','username'=>'xiaoqianjie_s3@isivy.com','password'=>'changchangabc','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s4@isivy.com','username'=>'xiaoqianjie_s4@isivy.com','password'=>'changchangabc','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s5@isivy.com','username'=>'xiaoqianjie_s5@isivy.com','password'=>'changchangabc','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s6@isivy.com','username'=>'xiaoqianjie_s6@isivy.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s7@isivy.com','username'=>'xiaoqianjie_s7@isivy.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s8@isivy.com','username'=>'xiaoqianjie_s8@isivy.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s9@isivy.com','username'=>'xiaoqianjie_s9@isivy.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.qq.com",'setfrom'=>'xiaoqianjie_s10@isivy.com','username'=>'xiaoqianjie_s10@isivy.com','password'=>'aa1234567','port'=>25),
		);
	private $email_users_163 = array(
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s1@163.com','username'=>'xiaoqianjie_s1@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s2@163.com','username'=>'xiaoqianjie_s2@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s3@163.com','username'=>'xiaoqianjie_s3@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s4@163.com','username'=>'xiaoqianjie_s4@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s5@163.com','username'=>'xiaoqianjie_s5@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.126.com",'setfrom'=>'xiaoqianjie_s1@126.com','username'=>'xiaoqianjie_s1@126.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s6@163.com','username'=>'xiaoqianjie_s6@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s7@163.com','username'=>'xiaoqianjie_s7@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s8@163.com','username'=>'xiaoqianjie_s8@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s9@163.com','username'=>'xiaoqianjie_s9@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s10@163.com','username'=>'xiaoqianjie_s10@163.com','password'=>'aa1234567','port'=>25),
			array('host'=>"smtp.163.com",'setfrom'=>'xiaoqianjie_s11@163.com','username'=>'xiaoqianjie_s11@163.com','password'=>'aa1234567','port'=>25),
		);

	function __construct() {
		//$num = rand(0,count($this->email_users_qq)-1);
		//$this->mail_conf = (object)$this->email_users_qq[$num];

	}
	
	function choosemail() {

		if(strstr($this->toemail,'@163')){
			$usemail = $this->email_users_163;
		}else{
			$usemail = $this->email_users_qq;
		}
		
		$filename = $_SERVER['DOCUMENT_ROOT']."/runtime/mailsendl/{$this->taskname}_email_users_".$usemail[0][host];
		$handle = @fopen($filename, "r");
		if(!$handle){
			$handle = @fopen($filename, "w+");
			fwrite($handle, "0");
			$contents = 0;
		}else{
			$contents = fread($handle, filesize ($filename));
		}
		$handle = @fopen($filename, "w+");
		fwrite($handle, $contents>=11 ? 0 : $contents+1);		
		fclose($handle);
		$this->mail_conf = (object)$usemail[$contents];		
	}

	public function send()
	{
		$this->choosemail();
		include_once(dirname(__FILE__).'/PHPMailer/class.phpmailer.php');
		$mail = new PHPMailer();
		$mail->CharSet = "UTF-8"; 
        $mail->Host = $this->mail_conf->host;
        $mail->Port = $this->mail_conf->port;
        $mail->IsSMTP();
        $mail->SMTPAuth= true; 
        $mail->Username = $this->mail_conf->username;
        $mail->Password = $this->mail_conf->password;
        $mail->SetFrom($this->mail_conf->setfrom,$this->SetFromname);
        $mail->AddAddress($this->toemail,$this->toname);
		$mail->IsHTML(true);
        $mail->Subject = $this->subject;
        $mail->Body = $this->body;
		if($mail->Send())
			return true;
		else
			return false;
	}

}
